package de.bazinga.rotw.vehicle;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.GameMessage;
import de.bazinga.rotw.network.message.HitMessage;
import de.bazinga.rotw.network.message.NewBulletMessage;
import de.bazinga.rotw.person.OpponentList;
import de.bazinga.rotw.person.Person;

public class VehicleHitMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	private int personHitIndex = 0;
	
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof VehicleHitMessage)) return ;
		
		if(server) {
			Person p = null;
			if(getPersonHitIndex() == 0) {
				// This should be the Serverplayer!
				p = game.getPlayer();
			}else{
				p = OpponentList.getInstance().getOpponent(getPersonHitIndex());
			}
			Person player = OpponentList.getInstance().getOpponent(getPlayerId());
        	
        	p.hit(player, 100);
            
            HitMessage hitMessage = new HitMessage();
            hitMessage.setPlayerName(player.getPlayerName());
            hitMessage.setPlayerLocation(player.getCameraPosition());
            hitMessage.setDamage(100);
            hitMessage.setWeaponFlag(NewBulletMessage.VEHICLE);
                
            ((Server) game.getSyncManager()).getServer().sendToPlayer(hitMessage, p.getPlayerId());
		}
	}


	public void setPersonHitIndex(int personHitIndex) {
		this.personHitIndex = personHitIndex;
	}


	public int getPersonHitIndex() {
		return personHitIndex;
	}
}